<template>
    <van-nav-bar title="新增收货地址" left-arrow @click-left="onBack" />

    <van-address-edit :area-list="areaList" show-set-default :address-info="addressInfo"
        :area-columns-placeholder="['请选择', '请选择', '请选择']" @save="onSave" />
</template>
<script setup>
import { areaList } from '@vant/area-data';
import { useUserStore } from '@/store/user.js'
import { storeToRefs } from 'pinia'
import { reactive } from 'vue'
import apiList from '@/api'
import resJudge from '@/hooks/res';
import { useRouter,useRoute } from 'vue-router';

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
let { userInfo } = storeToRefs(userStore)
let addressInfo = reactive({
    name: userInfo.value.nickname,
    tel: userInfo.value.mobile
})

//导航条返回
const onBack = () => history.back()

//保存
async function onSave(info) {
    let data = {
        busid: userInfo.value.id,
        consignee: info.name,
        mobile: info.tel,
        status: info.isDefault === true ? 1 : 0,
        address: info.addressDetail,
        code: info.areaCode
    }
    let res = await apiList.AddressAdd(data)
    if (resJudge(res)) {
        if (route.query.action == 'order' || route.query.action == 'productInfo') {
            router.back()
        }else{
            router.push('/business/address/index')
        }
    }
}
</script>
